import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5
import QtQml.Models 2.13
import QtQuick.Controls.Styles 1.4
import et.dirsearch 1.0
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2

Item {
    id:root
    visible: true
    anchors.fill: parent

    ColumnLayout{
        spacing: 2

        RowLayout{
            id:firstrow
            Layout.preferredHeight: 30
            Layout.margins: 1
            spacing: 2
            Label{
                text: "CL3115"
            }

            Button{
                text: qsTr("刷新");
                Layout.preferredHeight: 25;
                Layout.preferredWidth: 80;
                onClicked: {
                    runBusyDialog();
                    console.log("refresh log");
                    dirsearch.searchFiles();
                    tableView.records=null;
                    tableView.records=dirsearch;
                    endBusyDialog();
                }
            }
            Button{
                text: qsTr("导出xlsx");
                Layout.preferredHeight: 25;
                Layout.preferredWidth: 100;
                onClicked: {
                    runBusyDialog();
                    console.log("records log");
                    dirsearch.exportExcel(1);
                }
            }
            Button{
                text:qsTr("删除");
                Layout.preferredHeight: 25;
                Layout.preferredWidth: 100;
                onClicked: {
                    msgboxDelete.open();
                }
            }

        }

        ResultsTableView{
            id:tableView
            Layout.fillWidth: true;
            Layout.fillHeight: true;
            Layout.preferredWidth: root.width;
            Layout.preferredHeight: root.height-firstrow.height;

        }

    }

    Component.onCompleted: {
        dirsearch.searchFiles();
        tableView.records=dirsearch;
    }

    Component{
        id:btnStyle;
        ButtonStyle{
            background: Rectangle{
                implicitWidth: 120;
                implicitHeight: 25;
                color: "#DDDDDD";
                border.width: control.hovered ? 2 : 1;
                border.color: control.hovered ? "green" : "#888888";
            }
        }
    }
    Connections{
        target: dirsearch            //在外部连接MouseArea的点击信号
        onExcelFinished:{
            showMsgBox();
            console.log("onexcelFinished");
        }
        onExcelIdle:{
            endBusyDialog();
        }
    }

    BusyIndicator{
        id:busyDialog
        visible: false
        anchors.centerIn: parent
        running: false
    }

    Dirsearch{
        id:dirsearch
    }

    function runBusyDialog(){
        busyDialog.running=true;
        busyDialog.visible=true;
    }
    function endBusyDialog(){
        busyDialog.running=false;
        busyDialog.visible=false;
    }
    function showMsgBox(){
        msgbox.open();
    }
    MessageDialog{
        id:msgbox
        icon: StandardIcon.Information;
        standardButtons: StandardButton.Ok;
        title: qsTr("提示");
        text: qsTr("完成");
    }
    MessageDialog{
        id:msgboxDelete
        icon: StandardIcon.Information;
        standardButtons: StandardButton.Yes | StandardButton.No
        modality: Qt.ApplicationModal;
        title: qsTr("提示");
        text: qsTr("确认删除选中记录");
        onYes: {
            runBusyDialog();
            console.log("records log delete");
            dirsearch.deleteRecords();
        }
    }
}
